package com.jee.ssm.modules.order.app;

import com.jee.ssm.model.Order;
import com.jee.ssm.model.PassData;
import com.jee.ssm.model.PayType;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.modules.order.services.OrderService;
import com.jee.ssm.modules.passData.services.PassDataService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单管理 客户端 Controller
 *
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/app/order")
public class AppOrderController {

    /**
     * 根据手持机编号获取当日已支付金额
     *
     * @param sbCode
     * @return
     */
    @RequestMapping(value = "/getPayMoney")
    @ResponseBody
    public Tip getPayMoney(String sbCode) {
        Tip tip = new Tip();
        Map map = new HashMap<>();

        Order order = new Order();
        order.setSbCode(sbCode);

        PassData passData = new PassData();
        passData.setSbCode(sbCode);

        List<Order> orderList = orderService.selectBySbCode(order);
        List<PayType> payTypes = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            PayType o = new PayType();
            o.setPayType(i);
            if (i == 0) {
                o.setPayTypeS("支付宝");
            } else if (i == 1) {
                o.setPayTypeS("微信");
            } else if (i == 2) {
                o.setPayTypeS("余额");
            } else if (i == 3) {
                o.setPayTypeS("现金");
            } else if (i == 4) {
                o.setPayTypeS("场内支付");
            }
            o.setCount(0);
            payTypes.add(o);
        }
        for (Order od : orderList) {
            if (od.getPayType() == 0) {
                payTypes.get(0).setCount(payTypes.get(0).getCount() + 1);
            } else if (od.getPayType() == 1) {
                payTypes.get(1).setCount(payTypes.get(1).getCount() + 1);
            } else if (od.getPayType() == 2) {
                payTypes.get(2).setCount(payTypes.get(2).getCount() + 1);
            } else if (od.getPayType() == 3) {
                payTypes.get(3).setCount(payTypes.get(3).getCount() + 1);
            } else if (od.getPayType() == 4) {
                payTypes.get(4).setCount(payTypes.get(4).getCount() + 1);
            }
        }

        Double payMoney = 0.0;
        Double allPayMoney = 0.0;
        Double unPayMoney = 0.0;
        Double runAwayMoney = 0.0;

        Order pay= orderService.getPayMoney(order);
        Order allPay = orderService.getAllPayMoney(order);
        Order unPay= orderService.getUnPayMoney(order);
        Order runAway = orderService.getRunAway(order);
        Long countPassInfo = passDataService.getPassInfo(passData);
        if (pay != null){
            payMoney = pay.getOrderMoney();
        }
        if (allPay!= null){
            allPayMoney = allPay.getOrderMoney();
        }
        if (unPay != null){
            unPayMoney = unPay.getOrderMoney();
        }
        if (runAway != null){
            runAwayMoney = runAway.getOrderMoney();
        }
        map.put("countPassInfo",countPassInfo);
        map.put("payMoney", payMoney);
        map.put("allPayMoney", allPayMoney);
        map.put("unPayMoney", unPayMoney);
        map.put("runAwayMoney",runAwayMoney);
        map.put("payTypes",payTypes);

        tip.setData(map);

        return tip;
    }

    /**
     * 根据手持机编号获取当日已支付金额
     *
     * @param posUserId
     * @return
     */
    @RequestMapping(value = "/getMoneyByUser")
    @ResponseBody
    public Tip getMoneyByUser(String posUserId) {
        Tip tip = new Tip();
        Map map = new HashMap<>();

        Order order = new Order();
        order.setPosUserId(posUserId);

        PassData passData = new PassData();
        passData.setPosUserId(posUserId);

        List<Order> orderList = orderService.selectBySbCode(order);
        List<PayType> payTypes = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            PayType o = new PayType();
            o.setPayType(i);
            if (i == 0) {
                o.setPayTypeS("支付宝");
            } else if (i == 1) {
                o.setPayTypeS("微信");
            } else if (i == 2) {
                o.setPayTypeS("余额");
            } else if (i == 3) {
                o.setPayTypeS("现金");
            } else if (i == 4) {
                o.setPayTypeS("场内支付");
            }
            o.setCount(0);
            payTypes.add(o);
        }
        for (Order od : orderList) {
            if (od.getPayType() == 0) {
                payTypes.get(0).setCount(payTypes.get(0).getCount() + 1);
            } else if (od.getPayType() == 1) {
                payTypes.get(1).setCount(payTypes.get(1).getCount() + 1);
            } else if (od.getPayType() == 2) {
                payTypes.get(2).setCount(payTypes.get(2).getCount() + 1);
            } else if (od.getPayType() == 3) {
                payTypes.get(3).setCount(payTypes.get(3).getCount() + 1);
            } else if (od.getPayType() == 4) {
                payTypes.get(4).setCount(payTypes.get(4).getCount() + 1);
            }
        }

        Double payMoney = 0.0;
        Double allPayMoney = 0.0;
        Double unPayMoney = 0.0;
        Double runAwayMoney = 0.0;

        Order pay= orderService.getPayMoney(order);
        Order allPay = orderService.getAllPayMoney(order);
        Order unPay= orderService.getUnPayMoney(order);
        Order runAway = orderService.getRunAway(order);
        Long countPassInfo = passDataService.getPassInfo(passData);
        if (pay != null){
            payMoney = pay.getOrderMoney();
        }
        if (allPay!= null){
            allPayMoney = allPay.getOrderMoney();
        }
        if (unPay != null){
            unPayMoney = unPay.getOrderMoney();
        }
        if (runAway != null){
            runAwayMoney = runAway.getOrderMoney();
        }
        map.put("countPassInfo",countPassInfo);
        map.put("payMoney", payMoney);
        map.put("allPayMoney", allPayMoney);
        map.put("unPayMoney", unPayMoney);
        map.put("runAwayMoney",runAwayMoney);
        map.put("payTypes",payTypes);

        tip.setData(map);

        return tip;
    }

    //---------------------------- property -------------------------------

    @Resource
    private OrderService orderService;

    @Resource
    private PassDataService passDataService;

}
